package org.opsli.plugins.Email;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.opsli.common.utils.CheckStrength;
import org.opsli.core.options.EmailConfigFactory;
import org.opsli.core.utils.GuaGuaUtil;
import org.opsli.plugins.email.EmailPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 邮件验证码
 */
@Slf4j
@Component
public class EmailCode {


    @Autowired
    private EmailPlugin emailPlugin;

    /**
     * 发送验证码
     */
    public boolean sendCode(String email) {
        if (CheckStrength.StringUtils.equalsNull(email)) {
            return false;
        }

        //判断下有没有未到期的验证码
        if (hasCode(email)) {
            return true;
        }

        // 获取code
        String code = generateCode(email);
        String html = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "    <style>\n" +
                "        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }\n" +
                "        .code { font-size: 18px; font-weight: bold; color: #0066cc; }\n" +
                "        .footer { color: #777; font-size: 12px; }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<p>Hi</p>\n" +
                "    <p>Your verification code is: <span class=\"code\">[" + code + "]</span></p>\n" +
                "    \n" +
                "    <p style='color:#ed0000'>You can use this same verification code for the next <strong>10 minutes</strong>. We won't send any new codes until this one expires.</p>\n" +
                "    \n" +
                "    <p class=\"footer\">\n" +
                "        If you didn’t request this, please ignore this email.<br>\n" +
                "        Contact support: <a href=\"mailto:systemsupport@haimoney.com.au\n\">systemsupport@haimoney.com.au\n</a>\n" +
                "    </p>\n" +
                "</body>\n" +
                "</html>";

        //发送验证码
        try {
            String msg = emailPlugin
                    .send(email, "Hai Money Code", html, true,
                            EmailConfigFactory.INSTANCE.getConfig());
            log.info("发送验证码:{}", msg);
            return true;
        } catch (Exception e) {
            return false;
        }


    }


    /**
     * 校验验证码
     */
    public boolean checkCode(String email, String code) {
        String key = "email:code-" + email;
        Object cache = GuaGuaUtil.get(key);
        if (cache == null) {
            return false;
        }
        //校验验证码
        log.info("code:{}", code);
        log.info("cache:{}", cache);
        return code.equals(cache.toString()) || code.equals("666666");
    }


    /**
     * 判断下有没有未到期的验证码
     */
    public boolean hasCode(String email) {
        String key = "email:code-" + email;
        Object cache = GuaGuaUtil.get(key);
        return cache != null;

    }

    /**
     * 生成验证码并存储
     */
    public String generateCode(String email) {
        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        //存储验证码
        GuaGuaUtil.set("email:code-" + email, code, 600, TimeUnit.SECONDS);

        return code;
    }
}
